home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
HamCall (October 1991)
/
HamCall (Whitehall Publishing)(1991).bin
/
prgming
/
ctutor
/
chap01.txt
< prev
next >
Wrap
Text File
|
1990-10-14
|
8KB
|
232 lines
Chapter 1
GETTING STARTED
WHAT IS AN IDENTIFIER
______________________________________________________________
Before you can do anything in any language, you must at least
know how to name an identifier. An identifier is used for any
variable, function, data definition, etc. In the programming
language C, an identifier is a combination of alphanumeric
characters, the first being a letter of the alphabet or an
underline, and the remaining being any letter of the alphabet,
any numeric digit, or the underline. In the case of some
compilers, a dollar sign is permitted but not as the first
character of an identifier. It should be pointed out that
even though a dollar sign may be permitted by your C compiler,
it is not used anywhere in this tutorial since it is not in
general use by C programmers, and is not even allowed by most
compilers. If you do not plan to write any portable code, you
can use it at will if you feel it makes your code more
readable.
Two rules must be kept in mind when naming identifiers.
1. The case of alphabetic characters is significant. Using
"INDEX" for a variable is not the same as using "index"
and neither of them is the same as using "InDeX" for a
variable. All three refer to different variables.
2. As C is defined, up to 32 significant characters can be
used and will be considered significant by most
compilers. If more than 32 are used, they will be
ignored by the compiler.
WHAT ABOUT THE UNDERLINE?
______________________________________________________________
Even though the underline can be used as part of a variable
name, and adds greatly to the readability of the resulting
code, it seems to be used very little by experienced C
programmers. It adds greatly to the readability of a program
to use descriptive names for variables and it would be to your
advantage to do so. Pascal programmers tend to use long
descriptive names, but most C programmers tend to use short
cryptic names. Most of the example programs in this tutorial
use very short names for that reason.
1-1
Chapter 1 - Getting Started
KEYWORDS
______________________________________________________________
There are 32 words defined as keywords in C. These have
predefined uses and cannot be used for any other purpose in
a C program. They are used by the compiler as an aid to
compiling the program. They are always written in lower case.
A complete list follows;
auto double int struct
break else long switch
case enum register typedef
char extern return union
const float short unsigned
continue for signed void
default goto sizeof volatile
do if static while
In addition to this list of keywords, your compiler may use
a few more. If it does, they will be listed in the
documentation that came with your compiler. Each of the above
keywords will be illustrated and used in this tutorial.
WE NEED DATA AND A PROGRAM
______________________________________________________________
Any computer program has two entities to consider, the data,
and the program. They are highly dependent on one another and
careful planning of both will lead to a well planned and well
written program. Unfortunately, it is not possible to study
either completely without a good working knowledge of the
other. For this reason, this tutorial will jump back and
forth between teaching methods of program writing and methods
of data definition. Simply follow along and you will have
a good understanding of both. Keep in mind that, even though
it seems expedient to sometimes jump right into the program
coding, time spent planning the data structures will be well
spent and the final program will reflect the original
planning.
HOW THIS TUTORIAL IS WRITTEN
______________________________________________________________
As you go through the example programs, you will find that
every program is complete. There are no program fragments
that could be confusing. This allows you to see every
requirement that is needed to use any of the features of C as
they are presented. Some tutorials I have seen give very few,
and very complex examples. They really serve more to confuse
the student. This tutorial is the complete opposite because
it strives to cover each new aspect of programming in as
simple a context as possible. This method, however, leads to
1-2
Chapter 1 - Getting Started
a lack of knowledge in how the various parts are combined.
For that reason, the last chapter is devoted entirely to using
the features taught in the earlier chapters. It will
illustrate how to put the various features together to create
a usable program. They are given for your study, and are not
completely explained. Enough details of their operation are
given to allow you to understand how they work after you have
completed all of the previous lessons.
RESULT OF EXECUTION
______________________________________________________________
The result of executing each program will be given in comments
at the end of the program listing, after the comment is
defined in about the fourth program of chapter 2. If you feel
confident that you completely understand the program, you can
simply refer to the result of execution to see if you
understand the result. In this case, it will not be necessary
for you to compile and execute every program. It would be a
good exercise for you to compile and execute some of them
however, because all C compilers will not generate exactly the
same results and you need to get familiar with your own
compiler.
At this point, you should load and run =============
FIRSTEX.C if you have not yet done so, to see FIRSTEX.C
that your C compiler is properly loaded and =============
operating.
A DISCUSSION OF SOME OF THE FILES
______________________________________________________________
LIST.EXE
This file will list the source files for you with line numbers
and filename. To use it, simply type "LIST" followed by the
appropriate filename. Type LIST FIRSTEX.C now for an example.
C source code is given in Chapter 14 for a similar listing
program along with a brief description of its operation.
After you have completed your study of C, you will have the
ability to read and understand the source code for this
program.
PRINTALL.BAT
This is a batch file that will call the above LIST.EXE file
once for each of the example C programs, printing all of the
files out. If you want a hardcopy of all of the files, enter
PRINTALL and watch as your printer fills about 70 sheets of
paper with C programs.
1-3
Chapter 1 - Getting Started
THE \ANSWER DIRECTORY
______________________________________________________________
There is a directory on the distribution disk named ANSWER
which contains an answer to each of the programming exercises
given at the end of the chapters. You should attempt to do
original work on each of the exercises before referring to
these answers, in order to gain your own programming
experience. These answers are given for your information in
case you are completely stuck on how to solve a particular
problem. These answers are not meant to be the only answer,
since there are many ways to program anything, but they are
meant to illustrate one way to solve the suggested programming
problem.
The answers are all in executable files named in the format
CHnn_m.C where nn is the chapter number, and m is the exercise
number. If there is more than one answer required, an A, B,
or C is included following the exercise number.
1-4